﻿// Original author contact info: Owen Emlen (owene_1998@yahoo.com)
// Note: other individuals may also have contributed to this code
// Project hosted on CodePlex.com as of 1/10/2009 at http://wpfimageviewer.codeplex.com
using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.ComponentModel;
using System.Reflection;
using System.Windows;
using BrainTechLLC;

namespace EventWindowHandler.Plugin
{    
    public class ObjectEventInfoViewModel : ObservableCollection<EventCallInfoViewModel>
    {
        public ObjectEventInfo _info;

        public ObjectEventInfoViewModel(ObjectEventInfo info)
        {
            _info = info;

            var all = info.EventInfo.AllItems;

            for (int n = 0; n < all.Count; n++)
            {
                var vm = AddViewModel(all[n]);
            }
        }

        protected EventCallInfoViewModel AddViewModel(EventCallInfo eventInfo)
        {
            EventCallInfoViewModel vm = new EventCallInfoViewModel(eventInfo, _info.ObjectInstance, _info.Type);
            this.Add(vm);
            return vm;
        }

        public List<EventCallInfo> ReadCurrentEventCallInfos()
        {
            List<EventCallInfo> results = new List<EventCallInfo>();

            for (int n = 0; n < this.Count; n++)
            {
                results.Add(this[n].Item);
            }

            return results;
        }

        public void ReconcileListDifferences(List<EventCallInfo> newList)
        {
            var oldList = ReadCurrentEventCallInfos();
            List<EventCallInfo> itemsAdded;
            List<EventCallInfo> itemsRemoved;

            if (!newList.CompareCollections(oldList, out itemsAdded, out itemsRemoved))
            {
                foreach (var removed in itemsRemoved)
                {
                    this.RemoveAll(true, (vm) => vm.Item == removed);
                }

                foreach (var added in itemsAdded)
                {
                    AddViewModel(added);
                }
            }
        }

        public int EventCallCount
        {
            get { return _info.EventInfo.Count; }
        }
    }
}
